import React from "react";
import { Button, Switch, message } from "antd";

/*****
 *
 * updateStatusFunc 修改状态的函数
 * record 当前表格数据行
 * actionRef 表格ref
 * recordField record里面需要取id的上级字段名
 *
 * eg: useUpdateStatus(updateStatus, record, actionRef, "admin")
 */
export default (
  updateStatusFunc,
  record,
  actionRef,
  recordField,
  disabled,
  statusField = "status"
) => {
  // 修改状态
  const handleUpdateStatus = async (status, row, field) => {
    const postData = {
      id: recordField ? row[recordField].id : row.id,
      [field]: status ? 1 : 0,
    };
    const res = await updateStatusFunc(postData);
    if (res.data) {
      message.success("操作成功");
      actionRef?.current.reload();
    }
  };

  const status = recordField
    ? record[recordField][statusField]
    : record[statusField];

  return (
    <Switch
      disabled={disabled}
      onChange={(e) => {
        handleUpdateStatus(e, record, statusField);
      }}
      checked={status === 1}
    />
  );
  // (
  //   <>
  //     {status === 1 ? (
  //       <Button
  //         key="disable"
  //         type="text"
  //         danger
  //         onClick={() => {
  //           handleUpdateStatus(9, record, "status");
  //         }}
  //       >
  //         禁用
  //       </Button>
  //     ) : (
  //       <Button
  //         key="enable"
  //         type="text"
  //         onClick={() => {
  //           handleUpdateStatus(1, record, "status");
  //         }}
  //       >
  //         启用
  //       </Button>
  //     )}
  //   </>
  // );
};
